import 'package:path/path.dart' as p;
import 'dart:convert';
import 'dart:html';

class ConfigUtils {
  static Config config = Config.defaultData();

  static loadServerConfig() async {
    String textPath = p.join(p.current, 'config.json');
    var request = await HttpRequest.request(textPath);
    String jsonString = request.response.toString();

    try {
      ConfigUtils.config = Config.fromRawJson(jsonString);
      print('config.json  读取完成');
    } catch (e) {
      print('config.json 转化数据配置失败');
    }
  }
}

class Config {
  String url;

  Config({
    required this.url,
  });

  factory Config.defaultData() => Config(url: '');

  factory Config.fromRawJson(String str) => Config.fromJson(json.decode(str));

  String toRawJson() => json.encode(toJson());

  factory Config.fromJson(Map<String, dynamic> json) =>
      Config(url: json["url"]);

  Map<String, dynamic> toJson() => {"url": url};
}
